function GetQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null)return unescape(r[2]);
    return null;
}

$(document).ready(function () {
    $.ajax({
        url:'/hotFixServer/patch/patchList?appVersionId=' + GetQueryString("id") + '&page=1&size=100',
        type:"GET",
        cache:false,
        success:function(data){
            for (var i = 0; i < data.model['patchList'].length; i++) {
                var id = data.model['patchList'][i]['id']
                var patchTable = "<tr id=" + data.model['patchList'][i]['id'] + ">\r\n" +
                    "<td>" + data.model['patchList'][i]['patch_name'] + "</td>\r\n" +
                    "<td>" + data.model['patchList'][i]['patch_size'] + "</td>\r\n" +
                    "<td>" + data.model['patchList'][i]['patch_code'] + "</td>\r\n" +
                    "<td>" + data.model['patchList'][i]['download_number'] + "</td>\r\n" +
                    "<td>" + data.model['patchList'][i]['compose_number'] + "</td>\r\n" +
                    "<td><a href='javascript:void(0)' style=\"color:#CD8162\" onclick=\"serchDetailPatch('" + id + "')\">查看详情</a>|<a href='javascript:void(0)' style=\"color:#CD8162\" onclick=\"lookLog('" + data.model['patchList'][i]['patch_md5'] + "')\">查看日志</a>|<a  style=\"color:#242424\" href='javascript:void(0)' onclick=\"deleteAppPatch('" + data.model['patchList'][i]['id'] + "')\">删除</a></td>\r\n" +
                    "</tr>";
                $("#patchTable").append(patchTable);
            }
        }
    });
});

/*function creatPatchold(){
 //计算文件md5
 var fileMd5=calculate();
 console.log(fileMd5);

 var patchName=$("#patchName").val();
 //var patchSize=$("#patchSize").val();
 //var patchPath=$("#patchPath").val();
 var patchDescription=$("#patchDescription").val();
 /*$.ajax({
 url: '/hotFixServer/patch/upload',
 type: 'POST',
 cache: false,
 data: new FormData($('#fileForm')[0]),
 processData: false,
 contentType: false,
 dataType:'json',
 success : function(data) {
 if (data.model['success'] == 0) {
 alert('上传成功');
 //请求添加补丁接口
 var patchSize=data.model['file']['size'];
 var patchPath=data.model['file']['path'];
 var postData={
 "patchName":patchName,
 "patchDescription":patchDescription,
 "patchSize":patchSize,
 "patchPath":patchPath,
 "appVersionId":GetQueryString("id"),
 }
 $.ajax({
 type:"POST",
 url:"/hotFixServer/patch/createPatch",
 data:postData,
 success: function(data){
 if(data.model['success']==0){
 alert('创建成功');
 $("#patchName").val("");
 $("#patchDescription").val("");
 $("#patchFile").val("");
 $("#myPatchModal").modal('hide');
 var patchTable="<tr id="+data.model['patchId']+">\r\n"+
 "<td>"+patchName+"</td>\r\n"+
 "<td>"+patchSize+"</td>\r\n"+
 "<td>"+data.model['patchCode']+"</td>\r\n"+
 "<td> 0 </td>\r\n"+
 "<td> 0 </td>\r\n"+
 "<td><a href='javascript:void(0)' style=\"color:#CD8162\" onclick=\"serchDetailPatch('"+data.model['patchId']+"')\">查看详情</a>|<a  style=\"color:#242424\" href='javascript:void(0)' onclick=\"deleteAppPatch('"+data.model['patchId']+"')\">删除</a></td>\r\n"+
 "</tr>";
 $("#patchTable").prepend(patchTable);
 }
 else if(data.model['success']==1){
 alert(data.model['info']);
 }
 else if(data.model['success']==-1){
 alert(data.model['info']);
 }

 }

 });

 }
 else if(data.model['success'] == -1){
 alert('服务器异常');
 }
 }
 });
 }*/

var patchId = "";
function serchDetailPatch(id) {
    patchId = id;
    $.get('/hotFixServer/patch/getPatchInfo?id=' + id, function (data) {
        var patch_name = data.model['patchInfo']['patch_name'];
        var description = data.model['patchInfo']['description'];
        var patch_size = data.model['patchInfo']['patch_size'];
        var patch_code = data.model['patchInfo']['patch_code'];
        var patch_path = data.model['patchInfo']['patch_path'];
        var download_number = data.model['patchInfo']['download_number'];
        var compose_number = data.model['patchInfo']['compose_number'];
        $('#patchDetailName').val(patch_name);
        $('#patchDetailName').attr("disabled", true);
        $('#patchDetailSize').val(patch_size);
        $('#patchDetailSize').attr("disabled", true);
        $('#patchDetailCode').val(patch_code);
        $('#patchDetailCode').attr("disabled", true);
        $('#patchDetailPath').val(patch_path);
        $('#patchDetailPath').attr("disabled", true);
        $('#patchDetailCompose').val(compose_number);
        $('#patchDetailCompose').attr("disabled", true);
        $('#patchDetailDownload').val(download_number);
        $('#patchDetailDownload').attr("disabled", true);
        $('#patchDetailDescription').val(description);
        $('#patchDetailDescription').attr("disabled", true);
        $('#myPatchDetailModal').modal('show');
    });
}

//编辑补丁详情
function displayPatch() {
    $('#patchDetailName').attr("disabled", false);
    $('#patchDetailDescription').attr("disabled", false);
    $('#displayBtn').hide();
    $('#updateBtn').show();
}

//更新补丁详情
function updatePatch() {
    var patchDetailName = $('#patchDetailName').val();
    var patchDetailDescription = $('#patchDetailDescription').val();
    var datas = {
        "patchId": patchId,
        "patchName": patchDetailName,
        "description": patchDetailDescription,
    }
    $.ajax({
        url: '/hotFixServer/patch/updatePatch',
        type: 'PUT',
        data: datas,
        success: function (data) {
            if (data.model['success'] == 0) {
                $("#myPatchDetailModal").modal('hide');
                layer.alert('更新成功', {
                    icon: 6,
                    skin: 'layer-ext-moon'
                });
                location.reload(false);
            }
            else if (data.model['success'] == 1) {
                layer.alert(data.model['info'], {
                    icon: 5,
                    skin: 'layer-ext-moon'
                });
            }
            else if (data.model['success'] == -1) {
                layer.alert(data.model['info'], {
                    icon: 5,
                    skin: 'layer-ext-moon'
                });
            }
        }
    });
}

//删除补丁
function deleteAppPatch(id) {
    var deleteData = {
        "patchId": id

    }
    layer.confirm('您确定删除吗？', {
        btn: ['确定','取消'] //按钮
    }, function(){
        $.ajax({
            url: '/hotFixServer/patch/deletePatch',
            type: "DELETE",
            data: deleteData,
            success: function (data) {
                if (data.model['success'] == 0) {
                    layer.alert(data.model['info'], {
                        icon: 6,
                        skin: 'layer-ext-moon'
                    });
                    $('#' + id).remove();
                }
                else if (data.model['success'] == 1) {
                    layer.alert(data.model['info'], {
                        icon: 5,
                        skin: 'layer-ext-moon'
                    });
                    location.reload(false);
                }
                else if (data.model['success'] == -1) {
                    layer.alert(data.model['info'], {
                        icon: 5,
                        skin: 'layer-ext-moon'
                    });
                    location.reload(false);
                }
            },
            error: function (xhr, textstatus, thrown) {

            }
        });
    }, function(){
        layer.closeAll();
    });

}

function back() {
    history.go(-1);
}


//计算文件md5
function creatPatch() {
    var fileValue = $('#patchFile').val();
    if (fileValue == "") {
        layer.alert('请上传文件', {
            icon: 5,
            skin: 'layer-ext-moon'
        });
    } else {
        var blobSlice = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice,
            file = document.getElementById("patchFile").files[0],
            chunkSize = 2097152, // read in chunks of 2MB
            chunks = Math.ceil(file.size / chunkSize),
            currentChunk = 0,
            spark = new SparkMD5.ArrayBuffer(),
            frOnload = function(e){
                spark.append(e.target.result); // append array buffer
                currentChunk++;
                if (currentChunk < chunks)
                    loadNext();
                else {
                    var fileMd5 = spark.end();
                    var patchName = $("#patchName").val();
                    var patchDescription = $("#patchDescription").val();
                    if (patchName == "") {
                        layer.alert('补丁名称不能为空', {
                            icon: 5,
                            skin: 'layer-ext-moon'
                        });
                    }
                    else if (patchDescription == "") {
                        layer.alert('补丁描述不能为空', {
                            icon: 5,
                            skin: 'layer-ext-moon'
                        });
                    }
                    else {
                        $.ajax({
                            url: '/hotFixServer/patch/upload',
                            type: 'POST',
                            cache: false,
                            data: new FormData($('#fileForm')[0]),
                            processData: false,
                            contentType: false,
                            dataType: 'json',
                            beforeSend: function () {
                                layer.load(0, {shade: [0.4, '#E8E8E8', true]});
                            },
                            success: function (data) {
                                if (data.model['success'] == 0) {
                                    //请求添加补丁接口
                                    var patchSize = data.model['file']['size'];
                                    var patchPath = data.model['file']['path'];
                                    var postData = {
                                        "patchName": patchName,
                                        "patchDescription": patchDescription,
                                        "patchSize": patchSize,
                                        "patchPath": patchPath,
                                        "appVersionId": GetQueryString("id"),
                                        "md5": fileMd5,
                                    }
                                    $.ajax({
                                        type: "POST",
                                        url: "/hotFixServer/patch/createPatch",
                                        data: postData,
                                        success: function (data) {
                                            if (data.model['success'] == 0) {
                                                layer.alert('创建补丁成功', {
                                                    icon: 6,
                                                    skin: 'layer-ext-moon'
                                                });
                                                $("#patchName").val("");
                                                $("#patchDescription").val("");
                                                $("#patchFile").val("");
                                                $("#myPatchModal").modal('hide');
                                                var patchTable = "<tr id=" + data.model['patchId'] + ">\r\n" +
                                                    "<td>" + patchName + "</td>\r\n" +
                                                    "<td>" + patchSize + "</td>\r\n" +
                                                    "<td>" + data.model['patchCode'] + "</td>\r\n" +
                                                    "<td> 0 </td>\r\n" +
                                                    "<td> 0 </td>\r\n" +
                                                    "<td><a href='javascript:void(0)' style=\"color:#CD8162\" onclick=\"serchDetailPatch('" + data.model['patchId'] + "')\">查看详情</a>|<a href='javascript:void(0)' style=\"color:#CD8162\" onclick=\"lookLog('" + fileMd5 + "')\">查看日志</a>|<a  style=\"color:#242424\" href='javascript:void(0)' onclick=\"deleteAppPatch('" + data.model['patchId'] + "')\">删除</a></td>\r\n" +
                                                    "</tr>";
                                                $("#patchTable").prepend(patchTable);
                                            }
                                            else if (data.model['success'] == 1) {
                                                layer.alert(data.model['info'], {
                                                    icon: 5,
                                                    skin: 'layer-ext-moon'
                                                });
                                            }
                                            else if (data.model['success'] == -1) {
                                                layer.alert(data.model['info'], {
                                                    icon: 5,
                                                    skin: 'layer-ext-moon'
                                                });
                                            }

                                        }

                                    });

                                }
                                else if (data.model['success'] == -1) {
                                    layer.alert(data.model['info'], {
                                        icon: 5,
                                        skin: 'layer-ext-moon'
                                    });
                                }
                            },
                            complete: function () {
                                layer.closeAll('loading');
                            },
                            error: function (data) {
                                console.info("error: " + data.responseText);
                            }
                        });

                    }
                }
            },
            frOnerror = function () {
                console.log("\noops, something went wrong.");
            };

        function loadNext() {
            var fileReader = new FileReader();
            fileReader.onload = frOnload;
            fileReader.onerror = frOnerror;
            var start = currentChunk * chunkSize,
                end = ((start + chunkSize) >= file.size) ? file.size : start + chunkSize;
            fileReader.readAsArrayBuffer(blobSlice.call(file, start, end));
        };
        loadNext();
    }

}

//查看日志
function lookLog(id) {
    location.href = "patchLog.html?id=" + id;
}


























